// pages/cart/cart.js
import { Search } from 'search-model.js'
var search = new Search();
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: null,
    goodsList:[],
    noneDate:false,
    hasMore:false,
    page:1,
    searchVal:'',
    focus:true,
    isFirst:false
  },
  onLoad: function (options) {
      let userInfo = wx.getStorageSync('userInfo')
      this.setData({
          userInfo: userInfo,
      })
    //   this.commonSearch('条纹',1)
  },
  search(e){
    let val = e.detail.value
    console.log(val)
    this.setData({
        searchVal:val,
        page:1
    })
    this.commonSearch(val,1,'search');
  },
  removeVal(){
      this.setData({
          searchVal: '',
          page: 1,
          focus:true
      })
  },
  onReachBottom() {
      var { page, hasMore, searchVal } = this.data;
      console.log('bottom',)
      /**
       * 此处加上数据大小的判断
       */
      if (hasMore) {
          this.setData({
              page: page + 1
          })
          this.commonSearch(searchVal,++page,'bottom')
      }
  },
  commonSearch(name,page,type){
      let { userInfo, goodsList } = this.data
        let params = {
            "data":{
                "page": page,
                "size": 10,
                "storeNo": userInfo.storeNo,
                "searchName": name
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        search.getSearch(params,res=>{
            if(res.status){
                let list = []
                console.log(res.data.total)
                if(res.total > 0){
                    res.data.forEach(val=>{
                        if (val.soldout == 0){
                            list.push(val)
                        }
                    })
                    if(goodsList && goodsList.length > 0 && type == 'bottom'){
                        list = [...goodsList, ...list]
                    }
                    console.log(list.length)
                    this.setData({
                        goodsList: list,
                        noneData:true,
                        hasMore: list.length > 5 && res.data.length > 5  ? true : false,
                        isFirst:true
                    })
                }else{
                    this.setData({
                        goodsList: [],
                        noneData: false,
                        hasMore: false,
                        isFirst: true
                        
                    })
                }
                wx.hideLoading()
            }else{
                wx.hideLoading()
                order.showMyTip({ _this: this, title: res.data.message })
            }
        })
  },
  onShow() {
    
  }
  
})